/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.util;

import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 作者：韩天
 * 创建时间：2011-8-3
 * 类描述：读取XML文件的工具类
 */
public class XMLReader {
    private String filePath = "";//xml文件所在的路径

    public XMLReader(String filePath) {
        this.filePath = filePath;
    }
    
    /**
     * 方法描述：从xml文件中读取excel表格中数据的位置
     * param：dataName——数据的名称
     * return：location: sheet、row、column——sheet号，行，列
    */
    public int[] getLocation(String dataName) {
	int sheet = 0;
        int row = 0;
        int column = 0;
	try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(filePath));
            Element rootElement = document.getRootElement();

            for (Iterator i = rootElement.elementIterator("data"); i.hasNext();) {
		Element data = (Element) i.next();
		if (data.elementText("name").equals(dataName)) {
                    sheet = Integer.parseInt(data.element("location").selectSingleNode("sheet").getText());
                    row = Integer.parseInt(data.element("location").selectSingleNode("row").getText());
                    column = Integer.parseInt(data.element("location").selectSingleNode("column").getText());
		}
            }
	} catch (Exception e) {
		e.printStackTrace();
	}
        int[] location = new int[3];
        location[0] = sheet;
        location[1] = row;
        location[2] = column;
	return location;
    }
}
